package com.esp.soccertutor.ui;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.esp.soccertutor.R;
import com.esp.soccertutor.uc.ApplicationBottom;
import com.esp.soccertutor.ui.adpater.ShowcaseAdapter;
import com.esp.soccertutor.ui.bean.BeanCategory;
import com.esp.soccertutor.ui.xmlparser.XMLParser_Category;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.Log;

public class ShowCaseActivity extends FragmentActivity{
	
	private String TAG = ShowCaseActivity.class.getCanonicalName();
	
	private ApplicationBottom applicationBottom;
	private Button btn_showcase,btn_Purchase;
	private int TabData,TypeID;
	private RadioButton rbtnEbooks,rbtnVideos;
	private RadioGroup mRadioGroup;
	private ListView mShowCaseView;
	private ProgressDialog mProgressDialog;
	private ArrayList<BeanCategory> arrylstBeanCategory;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Const.CONTEXT = ShowCaseActivity.this;
		setContentView(R.layout.showcase);
		
		applicationBottom = (ApplicationBottom)findViewById(R.id.applicationBottom);
		btn_showcase = applicationBottom.btn_showcase;
		btn_Purchase = applicationBottom.btn_MyPuarchase;
		
		btn_showcase.setBackgroundResource(R.drawable.btn_blue_background);
		btn_Purchase.setBackgroundResource(R.drawable.btn_dark_background);
		
		btn_showcase.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//TODO GO TO Show case 
				Intent mShowCaseIntent = new Intent(ShowCaseActivity.this,ShowCaseActivity.class);
				mShowCaseIntent.putExtra(Const.SHOWCASE, Const.INT_SHOWCASE);
				mShowCaseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mShowCaseIntent);
			}
		});
		
		btn_Purchase.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				//TODO GO TO Purchase 
//				Intent mPurchaseIntent = new Intent(ShowCaseActivity.this,MyPurchaseActivity.class);
//				mPurchaseIntent.putExtra(Const.PURCHASE, Const.INT_PURCHASE);
//				mPurchaseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				startActivity(mPurchaseIntent);
				
				Intent mPurchaseIntent = new Intent(ShowCaseActivity.this,Login.class);
				startActivity(mPurchaseIntent);
			}
		});
		
		
		rbtnEbooks = (RadioButton)findViewById(R.id.rbtn_ebooks);
		rbtnVideos = (RadioButton)findViewById(R.id.rbtn_videos);
		mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch (group.getCheckedRadioButtonId()) {
				case R.id.rbtn_ebooks:
					Log.print(TAG, "Ebooks");
					mShowCaseView.setAdapter(null);
					new TaskShowCase().execute();
					break;
				case R.id.rbtn_videos:
					Log.print(TAG, "Videos");
					mShowCaseView.setAdapter(null);
					new TaskShowCase().execute();
					break;
				default:
					break;
				}
			}
		});
		
		mShowCaseView = (ListView)findViewById(android.R.id.list);
		mShowCaseView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
					
					Log.print(TAG,"Item : " + arrylstBeanCategory.get(position).getId());
					int i = rbtnEbooks.isChecked()? Const.TYPEID_EBOOKS : Const.TYPEID_VIDEOS;
					Log.print(TAG,"Type "+ i);
					Intent mShowCaseIntent = new Intent(ShowCaseActivity.this,ShowCaseProducts.class);
					mShowCaseIntent.putExtra(Const.SHOWCASE, Const.INT_SHOWCASE);
					mShowCaseIntent.putExtra(Const.TYPEID,rbtnEbooks.isChecked()? Const.TYPEID_EBOOKS : Const.TYPEID_VIDEOS);
					mShowCaseIntent.putExtra(Const.CATEGORY_ID,arrylstBeanCategory.get(position).getId());
					mShowCaseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(mShowCaseIntent);
			}
		});
		
		mProgressDialog = new ProgressDialog(ShowCaseActivity.this);
		mProgressDialog.setMessage(getString(R.string.ProgressMessage));
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setCancelable(false);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//TODO CATCH INTENT DATA HERE
		TabData = getIntent().getIntExtra(Const.SHOWCASE,Const.INT_SHOWCASE);
		TypeID = getIntent().getIntExtra(Const.TYPEID,Const.TYPEID_EBOOKS);
		if(TabData == Const.INT_SHOWCASE){
			
			btn_showcase.setBackgroundResource(R.drawable.btn_blue_background);
			btn_Purchase.setBackgroundResource(R.drawable.btn_dark_background);
		}
		if(TypeID == Const.TYPEID_EBOOKS){
			//we reach here if type is books
			rbtnEbooks.setChecked(true);
		}else{
			rbtnVideos.setChecked(true);
		}
		
		new TaskShowCase().execute();
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private class TaskShowCase extends AsyncTask<Void, Void, Integer>{

		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog.show();
		}
		@Override
		protected Integer doInBackground(Void... params) {
			int Result = 0;
			try
			{
				//CALL XMLParser class To get XML 
				XMLParser_Category mParser = new XMLParser_Category(Const.BOOK_URL);
				
				if(XMLParser_Category.STATUS_CODE == 200){
					
					//WE REACH HERE WE GET DOCUMENT 
					arrylstBeanCategory = new ArrayList<BeanCategory>();
					arrylstBeanCategory = mParser.getCategory(XMLParser_Category.GetDocument());
					
				}else if(XMLParser_Category.STATUS_CODE == 400){
					return Result = 400;
					
				}else if(XMLParser_Category.STATUS_CODE == 500){
					return Result = 500;
				}
				
				
			}catch (Exception e) {
				e.printStackTrace();
				Result = 500;
			}
			return Result;
		}
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			mProgressDialog.dismiss();
			if(result == 0){
				//WE HERE FOR SET LIST VIEW ADAPTER
				
				System.out.println("[ SIZE OF ARRAY LIST " + arrylstBeanCategory.size());
				if(arrylstBeanCategory != null && arrylstBeanCategory.size() > 0){
					
					mShowCaseView.setAdapter(new ShowcaseAdapter(ShowCaseActivity.this,arrylstBeanCategory));
				}
				
			}
		}
		
	}
}
